<?php
/*
Copyright DHTMLX LTD. http://www.dhtmlx.com
This version of Software is free for using in non-commercial applications.
For commercial use please contact sales@dhtmlx.com to obtain license
*/


//start session to build different trees for different sessions (if you set $_SESSION["id"] to some hardcoded value, this way of processing will be skipped)
//session_start();
//if(!isset($_SESSION["id"]))
	//$_SESSION["id"] = microtime();

//include db connection settings
//require_once("file:///C|/xampp/common/config.php");
require("../start.php");  
	

	//$table = "todo";
	$table = $_REQUEST['table'];

//$link = mysql_pconnect($mysql_host, $mysql_user, $mysql_pasw);
//$db = mysql_select_db ($mysql_db);


//FUNCTIONS TO USE IN THE CODE LATER

//print tree XML based on parent_id (function calls itself to go through the nested levels)
	function getLevelFromDB($parent_id){

		//$table = "cms";
		global $table;

	global $domain;
	global $domainModus;
	
	$domainSql = "";
	if ($domainModus == "on") $domainSql = " AND domain = '".$domain."' ";



		//get tree level from database taking parent id as incomming argument               and GUID='".$_SESSION["id"]."'
		$sql = "SELECT  id, titel FROM ".$table." WHERE item_parent_id=$parent_id $domainSql ORDER BY item_order";
        //$sql = "SELECT id, titel FROM snapol_page WHERE item_parent_id=1 OR item_parent_id=0";
        
        
        if ($table == "snapol_menu") {
        $sql = "SELECT * FROM (select titel,snapol_menu.* from snapol_menu,snapol_page where _snapol_page=snapol_page.id 
UNION 
SELECT titel,snapol_menu.* FROM snapol_menu,snapol2_app_page WHERE _snapol2_app_page = snapol2_app_page.id) a  WHERE item_parent_id=$parent_id $domainSql ORDER BY item_order";
        }
        
        
        //select * from (select titel,url,ajax,snapol_menu.* from snapol_menu,snapol_page where _snapol_page=snapol_page.id union select titel,url,ajax,snapol_menu.* from snapol_menu,snapol2_app_page where _snapol2_app_page = snapol2_app_page.id) a where item_parent_id=0 order by item
        
        
        
       // echo $sql;
        
        
		$res = mysql_query ($sql);
		if($res){
			while($row=mysql_fetch_array($res)){
				//create xml tag for tree node
				print("<item id='".$row['id']."' text=\"". str_replace('"',"&quot;",$row['titel'])."\">");
				//include child nodes
				getLevelFromDB($row['id']);
				//close xml tag for the node
				print("</item>");
			}
		}else{
			echo mysql_errno().": ".mysql_error()." at ".__LINE__." line in ".__FILE__." file<br>".$sql;
		}    
	}

//XML HEADER

//include XML Header (as response will be in xml format)
if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
 		header("Content-type: application/xhtml+xml"); } else {
 		header("Content-type: text/xml");
}
echo("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");               
?>

<!-- start tree xml -->
<tree id="0">
	
<?php
	//print tree XML
	//getLevelFromDB(0);
    checkLogin();
    global $userGrp;
    
    
    getLevelFromDB(0); 
    
    /*if ($userGrp < 40) {
    
    getLevelFromDB(1);
    }
    else {
       getLevelFromDB(0);   
    }   */
	
    
    //Close db connection
//	mysql_close($link);
?>

<!-- close tree xml -->
</tree>
